var app = getApp();
var util = require('../../../utils/util.js');
var ajax = require('../../../utils/ajax.js');
var common = require('../../../utils/common.js');
var PageBase = require('../../../common/PageBase.js');

Page(common.extendPage(PageBase, {
  data: {
    dataLoaded: false,
    userInfo: {},
    isLogin: false,
    status: 0, //红包团的状态
    isShowPop: true,
    isSelfTuan: 0, //是否自己开的团
    fissionid: '',
    fid: ''
  },

  onLoad: function (options) {
    common.initApp({
      loginSuc: () => {

        var {
          fissionid,fid
        } = this.options;

        //debugger;

        if (typeof (options.fissionid) != "undefined" && options.fissionid != "") {
          fissionid = JSON.parse(options.fissionid);
          console.log(fissionid);
          this.setData({
            fissionid: fissionid
          })
        };

        if (typeof (options.fid) != "undefined" && options.fid != "") {
          fid = JSON.parse(options.fid);
          console.log(fid);
          this.setData({
            fid: fid
          })
        };

        this.getShareRed(fissionid,fid);

      }
    });
  },

  //加入红包团
  joinTuan: function(){
    var fissionid = this.data.fissionid;
    var fid = this.data.fid;
    console.log(app.getUid());
    ajax.P({
      url: 'm=plugins&c=fissionpacket&a=join',
      data: {
        uid: app.getUid(),
        fid: fid || '',
        fissionid: fissionid || ''
      },
      success: (res) => {
          if(res.code == 1){
            util.showWarn(res.msg);
            return;
          };
          var data = res.data;
          console.log(data);
          this.setData({
            userList: data.userlist,
            fission: data.fission,
            isSelfTuan: data.fission.ishead,
            status: data.fission.issuccess
          })
      },
      fail: () => {

      }
    });
  },

  // 查询红包情况
  getShareRed: function (fissionid,fid) {
    ajax.P({
      url: 'm=plugins&c=fissionpacket&a=openfission',
      data: {
        uid: app.getUid(),
        fid: fid || '',
        fissionid: fissionid || ''
      },
      success: (res) => {
        var data = res.data;
        this.setData({
            userList: data.userlist,
            fission: data.fission,
            isSelfTuan: data.fission.ishead,
            status: data.fission.issuccess || 0,
            fissionid: data.fission.fissionid
        })

      },
      fail: () => {
        
      }
    });
  },

  _closePop() {
    this.setData({
      isShowPop: false
    })
  },

  onShareAppMessage: function (res) {
    //debugger;
    var id = this.data.fissionid;
    var fid = this.data.fid;
    return {
      title: "宝宝在吗？帮我一起拆108元红包，名额不多了！",
      path: 'pages/marketing/shareRed/index?fissionid=' + id + '&fid=' + fid,
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },

}));